<template>
  <div class="tab-control">
    <div
      class="tab-control-item"
      v-for="(item, index) in tabContext"
      @click="changeTab(index)"
      :key="index"
    >
      <span :class="{ active: currentIndex === index }">{{ item }}</span>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'TabControl',
  props: {
    tabContext: {
      type: Array,
      default() {
        return []
      }
    }
  },
  setup(_, { emit }) {
    // 定义下标
    let currentIndex = ref(0)
    // 定义改变tab的方法
    function changeTab(index) {
      currentIndex.value = index
      emit('changeTab', index)
    }
    return {
      currentIndex,
      changeTab
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-control {
  display: flex;
  height: 0.4rem;
  line-height: 0.4rem;
  top: 0.44rem;
  position: sticky;
  background: #fff;
  z-index: 100;
  .tab-control-item {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      height: 100%;
      padding: 0 0.15rem;
    }
  }
  .active {
    color: var(--color-tint);
    border-bottom: 0.04rem solid var(--color-tint);
  }
}
</style>